const app= getApp();
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        withStatus:1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('-----share- index--options----')
        console.log(options)
        console.log('----share index----options----');
        var enterOptions =wx.getEnterOptionsSync();
        console.log(enterOptions);
        var query=enterOptions.query;
        if(Object.keys(query).length === 0){
            console.log('----share index----query----');
            
        }else{
            console.log(query)
            if(query.hasOwnProperty('parent_id')){
                console.log('---has parent_id---');
                console.log(query.parent_id);
            }
            if(query.hasOwnProperty('store_id')){
                console.log('---has store_id---');
                console.log(query.store_id);
            }
            if(query.hasOwnProperty('help')){
                console.log('---has help---');
                console.log(query.help);
                this.setData({
                    help:query.help
                })
                wx.setStorageSync('help',query.help)
            }
            //console.log('none');
        }
        console.log('----share-- index----enterOptions----');
        let that=this;
        var parent_id=options.parent_id;
        var share=options.share;
        var store_id=options.id;
        var sn=options.sn;
        if(store_id!=undefined){
            
          console.log('store_id1=>'+store_id)
          this.setData({
            store_id:store_id
          });
          wx.setStorageSync('store_id',store_id);
      }else{
        store_id=options.store_id;
        if(store_id==undefined){
            store_id=wx.getStorageSync('store_id');
        }
        wx.setStorageSync('store_id',store_id);
        console.log('store_id2=>'+store_id)
        this.setData({
              store_id:store_id
            });        
      }
        if(share==undefined){
            share=0;
        }
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var open=wx.getStorageSync('open');
        if(open==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
        that.setData({
            share:share,
            capsule:app.globalData.capsule,
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl
          })
    },
      goBack:function(){
        wx.navigateBack();
      },
      goIndex:function(){
        wx.reLaunch({
          url: '../index/index',
        })
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
                phoneNumber: this.data.store.phone,
             })
        }else if(url=='coupon'){
            var date=util.formatTime(new Date());
            var face_value=e.currentTarget.dataset.face_value;
            var pay_money=e.currentTarget.dataset.pay_money;
            var yxq=e.currentTarget.dataset.yxq;
            var month=e.currentTarget.dataset.month;
           //  log.info("Time:"+date+",page:index,function:goto:,index"+index);
            this.getCoupon(face_value,month,yxq,pay_money);
        }else{
            if(index==1){
                wx.reLaunch({               
                    url: url,
                  })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
           
        }
        console.log(url);
    },
    toShare(){
        this.setData({
            share:1
        })
    },
    hideShare(){
        this.setData({
            share:0
        })
    },
    goWith(e){
        var open = wx.getStorageSync('open');
        var coupon = this.data.coupon;
        var withStatus = this.data.withStatus;
        let that = this;
        wx.showModal({
            title: '系统提示',
            content: '您确定要提现么？',
            confirmText:'确定提现',
            cancelText:'考虑一下',
            complete: (res) => {
              if (res.cancel) {
                
              }
          if (res.confirm) {
              if(withStatus==1){
  
             
                  this.setData({
                      withStatus:0
                  })
                  setTimeout(function () {
                   that.setData({
                       withStatus: 1
                   })
                 }, 2000) //阻止一秒点击多次
                  wx.request({
                      url: app.globalData.platUrl+'share/gotoWith',
                      data:{from:1,user_id:open.user_id,id:coupon.id,coupon_no:coupon.coupon_no},
                      success:res=>{    
                         // console.log(res);
                         var data=res.data;
                         if(data.code=200){
                            that.Index();
                              that.showModals(data.desc)
                              
                         }else{
                            that.Index();
                              that.showModals(data.desc)
                           
                         }
                      }
                  });
              }else{
                  wx.showToast({
                      title: '请请勿重新点击',
                    })
              }
              }
            }
          })
    },
    showModals:function(msg){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
                return false;
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
   
        onShow() {    
       
            //this.getIndex();
            var help = wx.getStorageSync('help');
            this.setData({
                help:help
            })
            this.Index();
            var role = wx.getStorageSync('role');
           console.log(role);
           this.update(role);
            var tabIndex;
            if(role==0){
              tabIndex=1;
            }else if(role==1){
              tabIndex=1;
            }else{
              tabIndex=1;
            }
               
            tabService.updateIndex(this, tabIndex);
          },
          update(role) {
            tabService.updateRole(this, role);
          },
          Index(){
            let that = this;
            var open = wx.getStorageSync('open');
            var help = wx.getStorageSync('help');
            var parent_id = wx.getStorageSync('parent_id');
            wx.request({
                url: app.globalData.platUrl+'share/coupon',
                data:{from:1,user_id:open.user_id,parent_id:parent_id},
                success:res=>{
                    console.log(res)
                    let data = res.data;
                    that.setData({
                        list:data.list,
                        rule:data.rule,
                        coupon:data.coupon,
                        shares:data.share,
                        parent:data.parent,
                        shareList:data.shareList,
                        shareTitle:data.shareTitle,
                        shareLogo:data.shareLogo,
                        shareIndex:data.shareIndex,
                        showImg:data.show
                    })
                }
            })

          },
          refuse(e){ 
          wx.setStorageSync('help', 0);
          this.setData({
              help:0
          })
        },
        help(e){
            var parent_id = wx.getStorageSync('parent_id');
            var open= wx.getStorageSync('open');
            let that = this;
            if(parent_id){
                wx.request({
                    url: app.globalData.platUrl+'share/help',
                    data:{from:1,user_id:open.user_id,parent_id:parent_id},
                    success:res=>{
                        console.log(res);
                        let data= res.data;
                      if(res.statusCode==200){
                        that.showModals(data.msg)
                      }
                    }
              })
            }
            this.setData({
                help:0
            });
            if(parent_id>0){
              wx.setStorageSync('help', 1);
            }
            
            wx.setStorageSync('show', 0)
        },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareTimeline(e){
        console.log(e);
        var title = this.data.shareTitle;
        var shareLogo = this.data.shareLogo;
        var open = wx.getStorageSync('open');
        var store_id = wx.getStorageSync('store_id');
        var parent_id=open.user_id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: '/pages/share/index?help=1&parent_id='+parent_id+'&store_id='+store_id,        
           imageUrl:shareLogo      
           }
       },
    onShareAppMessage(e) {
        var open = wx.getStorageSync('open');
        var parent_id=open.user_id;
        var store_id = wx.getStorageSync('store_id');
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
   　  　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/share/index?help=1&parent_id='+parent_id+'&store_id='+store_id,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/share/index?help=1&parent_id='+parent_id+'&store_id='+store_id;
   　　}
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})